package cn.puge.insurance.utils;

import com.ctrip.framework.apollo.Config;
import com.ctrip.framework.apollo.ConfigService;
import org.springframework.stereotype.Component;


/**
 * @Author: gaoruiqi
 * @Date: 2023/4/26 23:42
 * @Version 1.0
 * Apollo工具类
 */
@Component
public class ApolloUtil {

    //拿到全部的配置
    private static Config config = ConfigService.getAppConfig();


    public static String getInfornationFromApollo(String key, String namespace, String defaultValue) {
        Config config = ConfigService.getConfig(namespace);
        if (config == null) {
            return defaultValue;
        }
        return config.getProperty(key, defaultValue).trim();
    }

    public static String getPropertiesFromApplication(String key, String defaultValue) {
        return config.getProperty(key, defaultValue).trim();
    }


    public static long getLongFromApollo(String key, String nameSpace, long defaultValue) {
        Config config = ConfigService.getConfig(nameSpace);
        if (config == null) {
            return defaultValue;
        }
        return config.getLongProperty(key, defaultValue);
    }

    public static boolean getBooleanFromApollo(String key, String nameSpace, boolean defaultValue) {
        Config config = ConfigService.getConfig(nameSpace);
        if (config == null) {
            return defaultValue;
        }
        return Boolean.parseBoolean(config.getProperty(key, defaultValue + "").trim());
    }

    public static int getIntegerFromApollo(String key, String nameSpace, int defaultValue) {
        Config config = ConfigService.getConfig(nameSpace);
        if (config == null) {
            return defaultValue;
        }
        return config.getIntProperty(key, defaultValue);
    }

    public static double getDoubleFromApollo(String key, String nameSpace, double defaultValue) {
        Config config = ConfigService.getConfig(nameSpace);
        if (config == null) {
            return defaultValue;
        }
        return config.getDoubleProperty(key, defaultValue);
    }


}
